package com.bwie;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: zhaijiaqi
 * @Date: 2025/6/28 11:57
 * @Description:
 **/
public class BookList1 {


    private List<Book> bookList = new ArrayList<>();
    private Lock lock=new ReentrantLock();

    //添加
    public Book addBook(Book book) {
        lock.lock();
        bookList.add(book);
        System.out.println(book);
        lock.unlock();
        return book;
    }

    //删除
    public Book removeBook(int index) {
        lock.lock();
        System.out.println(index);
        lock.unlock();
        return bookList.remove(index);
    }

    //获取
    public Book getBook(int index) {
        lock.lock();
        System.out.println(index);
        lock.unlock();
        return bookList.get(index);
    }

    public void sout() {
        lock.lock();
        for (Book book : bookList) {
            System.out.println(
                    book
            );
        }
        lock.unlock();
    }

    @Test
    public void getLock(){
        BookList1 bookList1 = new BookList1();
        System.out.println("添加");
        bookList1.addBook(new Book(1,"title1","author1","publisher1"));
        bookList1.addBook(new Book(2,"title2","author2","publisher2"));
        bookList1.addBook(new Book(3,"title3","author3","publisher3"));
        Book book = bookList1.removeBook(1);
        System.out.println("删除");
        Book book1 = bookList1.getBook(1);
        System.out.println("获取");
    }

}
